package mall.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import mall.pojo.Category;

@Repository
public class CategoryDAOImpl implements CategoryDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void createCategory(Category category) {
		sessionFactory.getCurrentSession().save(category);
	}

	@Override
	public int updateCategory(Category category) {
		String hql="UPDATE Category SET name = :name , position = :position WHERE id = :id";
		Query query= sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter("name", category.getName());
		query.setParameter("position", category.getPosition());
		query.setParameter("id", category.getId());
		return query.executeUpdate();
	}

	@Override
	public List<Category> getListCategory() {
		String hql = "FROM Category";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return (List<Category>)query.list();
	}

	@Override
	public Category getCategory(int catId) {
		String hql = "FROM Category C WHERE C.id = '" + catId + "'";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return (Category)query.list().get(0);
	}

	@Override
	public List<Category> getCategoryShop(int shopId) {
		String hql="FROM Category C WHERE C.shop.id= "+shopId;
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return (List<Category>)query.list();
	}

}
